package com.ouc.openplatform.zykt.standardManage.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouc.openplatform.zykt.standardManage.dto.NJ_TestStandardTempletDTO;
import com.ouc.openplatform.zykt.standardManage.model.NJ_TestStandardTemplet;
import com.ouc.openplatform.zykt.standardManage.service.NJ_ClientTestStandardTempletService;
import com.ouc.openplatform.zykt.standardManage.service.NJ_TestStandardTempletService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class NJ_ClientTestStandardTempletServiceImpl implements NJ_ClientTestStandardTempletService {

	NJ_TestStandardTempletService testStandardTempletService;

	/**
	 * 对象注入
	 */
	public NJ_TestStandardTempletService getTestStandardTempletService() {
		return testStandardTempletService;
	}

	public void setTestStandardTempletService(NJ_TestStandardTempletService testStandardTempletService) {
		this.testStandardTempletService = testStandardTempletService;
	}

	/**
	 * 对象转换方法
	 */
	private NJ_TestStandardTempletDTO transformTstDTO(NJ_TestStandardTemplet testStandardTemplet) {
		NJ_TestStandardTempletDTO testStandardTempletDTO = new NJ_TestStandardTempletDTO();

		testStandardTempletDTO.setTemplate_id(testStandardTemplet.getTemplate_id());
		testStandardTempletDTO.setTemplate_code(testStandardTemplet.getTemplate_code());
		testStandardTempletDTO.setTemplate_name(testStandardTemplet.getTemplate_name());
		testStandardTempletDTO.setTemplate_use_area(testStandardTemplet.getTemplate_use_area());
		testStandardTempletDTO.setTemplate_type(testStandardTemplet.getTemplate_type());
		testStandardTempletDTO.setUpdate_emp_id(testStandardTemplet.getUpdate_emp_id());
		testStandardTempletDTO.setUpdate_emp_name(testStandardTemplet.getUpdate_emp_name());
		testStandardTempletDTO.setUpdate_date(testStandardTemplet.getUpdate_date());
		testStandardTempletDTO.setIsrt_emp_id(testStandardTemplet.getIsrt_emp_id());
		testStandardTempletDTO.setIsrt_emp_name(testStandardTemplet.getIsrt_emp_name());
		testStandardTempletDTO.setIsrt_date(testStandardTemplet.getIsrt_date());
		testStandardTempletDTO.setDelete_status(testStandardTemplet.getDelete_status());
		testStandardTempletDTO.setPart_type_id(testStandardTemplet.getPart_type_id());
		testStandardTempletDTO.setPart_type_name(testStandardTemplet.getPart_type_name());
		testStandardTempletDTO.setFixscope(testStandardTemplet.getFixscope());
		testStandardTempletDTO.setRelateddocument(testStandardTemplet.getRelateddocument());
		testStandardTempletDTO.setRemarks(testStandardTemplet.getRemarks());
		testStandardTempletDTO.setValiddeadline(testStandardTemplet.getValiddeadline());
		testStandardTempletDTO.setState(testStandardTemplet.getState());

		return testStandardTempletDTO;
	}

	private NJ_TestStandardTemplet transformTst(NJ_TestStandardTempletDTO testStandardTempletDTO) {
		NJ_TestStandardTemplet testStandardTemplet = new NJ_TestStandardTemplet();

		testStandardTemplet.setTemplate_id(testStandardTempletDTO.getTemplate_id());
		testStandardTemplet.setTemplate_code(testStandardTempletDTO.getTemplate_code());
		testStandardTemplet.setTemplate_name(testStandardTempletDTO.getTemplate_name());
		testStandardTemplet.setTemplate_use_area(testStandardTempletDTO.getTemplate_use_area());
		testStandardTemplet.setTemplate_type(testStandardTempletDTO.getTemplate_type());
		testStandardTemplet.setUpdate_emp_id(testStandardTempletDTO.getUpdate_emp_id());
		testStandardTemplet.setUpdate_emp_name(testStandardTempletDTO.getUpdate_emp_name());
		testStandardTemplet.setUpdate_date(testStandardTempletDTO.getUpdate_date());
		testStandardTemplet.setIsrt_emp_id(testStandardTempletDTO.getIsrt_emp_id());
		testStandardTemplet.setIsrt_emp_name(testStandardTempletDTO.getIsrt_emp_name());
		testStandardTemplet.setIsrt_date(testStandardTempletDTO.getIsrt_date());
		testStandardTemplet.setDelete_status(testStandardTempletDTO.getDelete_status());
		testStandardTemplet.setPart_type_id(testStandardTempletDTO.getPart_type_id());
		testStandardTemplet.setPart_type_name(testStandardTempletDTO.getPart_type_name());
		testStandardTemplet.setFixscope(testStandardTempletDTO.getFixscope());
		testStandardTemplet.setRelateddocument(testStandardTempletDTO.getRelateddocument());
		testStandardTemplet.setRemarks(testStandardTempletDTO.getRemarks());
		testStandardTemplet.setValiddeadline(testStandardTempletDTO.getValiddeadline());
		testStandardTemplet.setState(testStandardTempletDTO.getState());

		return testStandardTemplet;
	}

	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public JSONObject tstToJsonObj(NJ_TestStandardTemplet tst) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("template_id", tst.getTemplate_id());
		jsonObject.put("template_code", tst.getTemplate_code());
		jsonObject.put("template_name", tst.getTemplate_name());
		jsonObject.put("template_use_area", tst.getTemplate_use_area());
		jsonObject.put("template_type", tst.getTemplate_type());
		jsonObject.put("update_emp_id", tst.getUpdate_emp_id());
		jsonObject.put("update_emp_name", tst.getUpdate_emp_name());
		try {
			jsonObject.put("update_date", formatter.format(formatter.parse(tst.getUpdate_date())));
		} catch (Exception e) {
			// TODO: handle exception
		}
		// jsonObject.put("update_date", tst.getUpdate_date());
		jsonObject.put("isrt_emp_id", tst.getIsrt_emp_id());
		jsonObject.put("isrt_emp_name", tst.getIsrt_emp_name());
		try {
			jsonObject.put("isrt_date", formatter.format(formatter.parse(tst.getIsrt_date())));
		} catch (Exception e) {
			// TODO: handle exception
		}
		// jsonObject.put("isrt_date", tst.getIsrt_date());
		jsonObject.put("delete_status", tst.getDelete_status());
		jsonObject.put("part_type_id", tst.getPart_type_id());
		jsonObject.put("part_type_name", tst.getPart_type_name());
		jsonObject.put("fixscope", tst.getFixscope());
		jsonObject.put("relateddocument", tst.getRelateddocument());
		jsonObject.put("remarks", tst.getRemarks());
		try {
			jsonObject.put("validdeadline", formatter.format(formatter.parse(tst.getValiddeadline())));
		} catch (Exception e) {
			// TODO: handle exception
		}
		// jsonObject.put("validdeadline", tst.getValiddeadline());
		if (tst.getState() == 1) {
			jsonObject.put("state", "有效");
		} else {
			jsonObject.put("state", "无效");
		}

		System.out.println("jsonObject:" + jsonObject);
		return jsonObject;
	}

	public int[] setPage(String page, String rows, int total) {
		int[] pArry = new int[3];
		pArry[2] = total;
		// 当前页
		int intPage = Integer.parseInt((page == null || page == "0") ? "1" : page);
		// 每页显示条数
		int pageSize = Integer.parseInt((rows == null || rows == "0") ? "10" : rows);
		// page = (page==null||page==0)?1:page

		// pArry[0]=page;
		// pArry[1]=rows;

		if (pArry[2] > 0) {
			pArry[0] = (intPage - 1) * pageSize + 1;
			if (pArry[2] > intPage * pageSize) {
				pArry[1] = intPage * pageSize;
			} else {
				pArry[1] = pArry[2];
			}
		} else {
			pArry[0] = 1;
			pArry[1] = pageSize;
		}
		return pArry;// 返回起点和终点
	}

	/**
	 * 重写方法
	 */
	@Override
	public Object getAllTestStandard() {
		System.out.println("enter getData function");
		List<NJ_TestStandardTempletDTO> tstDTOList = new ArrayList<NJ_TestStandardTempletDTO>();
		List<NJ_TestStandardTemplet> tstList = testStandardTempletService.getAllTestStandard();

		JSONArray jsonArray = new JSONArray();
		for (NJ_TestStandardTemplet tst : tstList) {
			jsonArray.add(tstToJsonObj(tst));
		}

		return jsonArray;
	}

	@Override
	public Object searchAllTestStandard(String page, String rows) {
		int[] pArry = setPage(page, rows, testStandardTempletService.getSearchAllTestStandardCount()); // 索引

		List<NJ_TestStandardTemplet> tstList = testStandardTempletService.searchAllTestStandard(pArry[0], pArry[1]);
		Map<String, Object> result = new HashMap<String, Object>(2);
		JSONArray jsonArray = new JSONArray();
		for (NJ_TestStandardTemplet testStandardTemplet : tstList) {
			jsonArray.add(tstToJsonObj(testStandardTemplet));
		}
		result.put("total", pArry[2]);
		result.put("rows", jsonArray);

		return JSONObject.fromObject(result);
	}

	@Override
	public Object searchTestStandardByCondition(String std_type, int pd_type, String std_name, String std_code,
			String page, String rows) {
		int[] pArry = setPage(page, rows, testStandardTempletService.getSearchTestStandardByConditionCount(std_type,
				pd_type, std_name, std_code)); // 索引
		System.out.println("std_type:" + std_type + ",pd_type:" + ",std_name:" + std_name + ",std_code:" + std_code);

		List<NJ_TestStandardTemplet> tstList = testStandardTempletService.searchTestStandardByCondition(std_type,
				pd_type, std_name, std_code, pArry[0], pArry[1]);
		Map<String, Object> result = new HashMap<String, Object>(2);
		JSONArray jsonArray = new JSONArray();
		for (NJ_TestStandardTemplet testStandardTemplet : tstList) {

			jsonArray.add(tstToJsonObj(testStandardTemplet));

		}
		result.put("total", pArry[2]);
		result.put("rows", jsonArray);

		return JSONObject.fromObject(result);
	}

	@Override
	public int insertTestStandard(NJ_TestStandardTempletDTO testStandardTempletDTO) {
		NJ_TestStandardTemplet testStandardTemplet = new NJ_TestStandardTemplet();

		testStandardTemplet = transformTst(testStandardTempletDTO);
		return testStandardTempletService.insertTestStandard(testStandardTemplet);
	}

	@Override
	public int insertTestStandardByExl(String[][] data, String flag, String realname) {
		// TODO Auto-generated method stub
		System.out.println("lty tst导入Exl");
		List<NJ_TestStandardTemplet> tstL = new ArrayList<NJ_TestStandardTemplet>();
		int datarow = data.length; // 数组的总行数
		System.out.println("datarow:" + datarow);
		int result = 0;
		for (int i = 1; i < datarow; i++) {
			NJ_TestStandardTemplet testStandardTemplet = new NJ_TestStandardTemplet();
			if (data[i][1] == null || data[i][1] == "") {
				testStandardTemplet.setTemplate_name(" ");
			} else {
				testStandardTemplet.setTemplate_name(data[i][1]);
			}

			if (data[i][2] == null || data[i][2] == "") {
				testStandardTemplet.setTemplate_englishname(" ");
			} else {
				testStandardTemplet.setTemplate_englishname(data[i][2]);
			}
			if (data[i][3] == null || data[i][3] == "") {
				testStandardTemplet.setTemplate_code(" ");
			} else {
				testStandardTemplet.setTemplate_code(data[i][3]);
			}
			if (data[i][4] == null || data[i][4] == "") {
				testStandardTemplet.setTemplate_use_area(" ");
			} else {
				testStandardTemplet.setTemplate_use_area(data[i][4]);
			}
			if (data[i][5] == null || data[i][5] == "") {
				testStandardTemplet.setTemplate_type(" ");
			} else {
				testStandardTemplet.setTemplate_type(data[i][5]);
			}
			if (data[i][6] == null || data[i][6] == "") {
				testStandardTemplet.setPart_type_name(" ");
			} else {
				testStandardTemplet.setPart_type_name(data[i][6]);
			}

			try {
				testStandardTemplet.setPart_type_id(
						testStandardTempletService.getProductTypeByName(testStandardTemplet.getPart_type_name()));
			} catch (Exception e) {
				System.out.println("产品类别ID设置失败");
				testStandardTemplet.setPart_type_id(0);
			}
			if (data[i][7] == null || data[i][7] == "") {
				testStandardTemplet.setFixscope(" ");
			} else {
				testStandardTemplet.setFixscope(data[i][7]);
			}
			if (data[i][8] == null || data[i][8] == "") {
				testStandardTemplet.setIsrt_emp_name(" ");
			} else {
				testStandardTemplet.setIsrt_emp_name(data[i][8]);
			}

			try {
				testStandardTemplet.setIsrt_emp_id(testStandardTempletService.getUsernoByName(data[i][7]));
			} catch (Exception e) {
				System.out.println("添加人ID设置失败");
				testStandardTemplet.setIsrt_emp_id("0");
			}
			tstL.add(testStandardTemplet);
			// result=testStandardTempletService.importByExcel(testStandardTemplet);
		}

		result = testStandardTempletService.importByList(tstL);
		System.out.println("List 导入长度：" + tstL.size());
		return result;
	}

	@Override
	public int deleteTestStandard(int template_id) {
		System.out.println("impl:" + template_id);
		return testStandardTempletService.deleteTestStandard(template_id);
	}

	@Override
	public int editTestStandard(NJ_TestStandardTempletDTO testStandardTempletDTO) {
		NJ_TestStandardTemplet testStandardTemplet = new NJ_TestStandardTemplet();
		testStandardTemplet = transformTst(testStandardTempletDTO);
		System.out.println(testStandardTempletDTO.getRelateddocument());
		return testStandardTempletService.editTestStandard(testStandardTemplet);
	}

}
